package function;


import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.util.StringUtils;

import java.util.function.Function;

/**
 * @author G_Y
 * @version 1.0.0, 2022/3/29 14:22
 **/
public class FunctionTest {

    /**
     * @author G_Y, 2022/3/29 14:23
     **/
    public static void main(String[] args) {


    }

    static class QueryUserRequest {
        String userId;
    }

    /**
     * @author G_Y
     * @version 1.0.0, 2022/3/29 14:19
     **/
    static class UserClient extends AbstractClient<QueryUserRequest, Result<UserInfo>> {

        @Reference
        UserFacade userFacade;

        /**
         * @author G_Y, 2022/3/29 14:22
         **/
        Result<UserInfo> getUserInfo(QueryUserRequest request) {

            Result<UserInfo> userInfoResp = null;
            try {
                userInfoResp = userFacade.getUserInfo(request);
            } catch (Exception e) {
                // 网络异常处理
                return Result.failed("failed");
            }

            if (userInfoResp.isFailed()) {
                // 失败处理
                return Result.failed("failed");
            }

            if (StringUtils.isEmpty(userInfoResp.getData().getPhone())) {
                // 返回的数据不满足当前系统所需
                return Result.failed("failed");
            }

            return Result.success(userInfoResp.getData());
        }

        @Override
        Result<UserInfo> facadeInvoke(QueryUserRequest queryUserRequest) {
            return null;
        }

        @Override
        AbstractValidator<QueryUserRequest, Result<UserInfo>> getValidator() {
            return null;
        }
    }

    interface UserFacade {
        Result<UserInfo> getUserInfo(QueryUserRequest request);
    }

    abstract static class AbstractClient<T, R> {
        /**
         * @param
         * @author G_Y, 2022/3/29 14:22
         **/
        R invoke(T req, Function<T, R> function) {
            return function.apply(req);
        }

        R invoke(T req) {
            return this.facadeInvoke(req);
        }

        abstract R facadeInvoke(T t);

        abstract AbstractValidator<T, R> getValidator();

    }

    abstract static class AbstractValidator<T, R> {

    }

}
